using FairyGUI;
using UnityEngine;

public class Launch : MonoBehaviour
{
    GComponent m_root;
    GTextInput m_inputAccount;
    GTextInput m_inputPwd;
    GTextField atkText;
    GTextField defText;
    GTextField mpText;
    GTextField hpText;
    GButton m_confirmBtn;
    GButton m_cancelBtn;
    GButton m_addBtn;

    GButton atkBtn;
    GButton defBtn;
    GButton hpBtn;
    GButton mpBtn;
    // int maxValue = 100;
    int hp = 60;
    int atk = 50;
    int mp = 50;
    int def = 50;
    int strengh = 90;
    GList list;
    GList playerArmorList;
    GComponent _mainView;
    // BagWindow _bagWindow;
    PopUpWindow _popWindow;
    ArrangeWindow _arrangeWindow;
    DataManager dataManager;

    void Awake()
    {
        //Register custom loader class
        //UIObjectFactory.SetLoaderExtension(typeof(MyGLoader));
        dataManager = GetComponent<DataManager>();
    }

    void Start()
    {
        dataManager.readGameInfo();
        dataManager.addItem();
        _mainView = this.GetComponent<UIPanel>().ui;
        hpText = _mainView.GetChild("hpText").asTextField;
        mpText = _mainView.GetChild("mpText").asTextField;
        atkText = _mainView.GetChild("atkText").asTextField;
        defText = _mainView.GetChild("defText").asTextField;



        atkText.text = atk + "/" + 100;
        mpText.text = mp + "/" + 100;
        defText.text = def + "/" + 100;
        hpText.text = hp + "/" + 100;
        //UIPackage.AddPackage("Bag");
        // _bagWindow = new BagWindow();
        _arrangeWindow = new ArrangeWindow();
        _popWindow = new PopUpWindow(() => { Debug.Log("confirm"); }, () => { Debug.Log("cancel"); });


        /* _mainView.GetChild("arrangeBtn").onClick.Add(() => {

         });*/

        playerArmorList = _mainView.GetChild("armorsList").asList;
        Debug.Log(playerArmorList);
        refreshPlayerEquipList();
        _mainView.GetChild("closeBtn").onClick.Add(() =>
        {
            _arrangeWindow.Hide();

        });
    }

    void refreshList()
    {
        list = m_root.GetChild("alist").asList;
        list.SetVirtualAndLoop();
        list.itemRenderer = RenderListItem;

        list.numItems = 100;
    }

    void RenderListItem(int index, GObject obj)
    {
        GButton button = obj.asButton;
        button.title = "" + index;
    }

    void refreshPlayerEquipList()
    {
        //list = m_root.GetChild("armorsList").asList;
        //list.SetVirtualAndLoop();
        playerArmorList.itemRenderer = RenderPlayerEquipListItem;

        playerArmorList.numItems = 3;
    }

    void RenderPlayerEquipListItem(int index, GObject obj)
    {
        GButton button = obj.asButton;
        button.title = "" + index;

        //Debug.Log(button.title);
        GLoader spriteLoader2 = button.GetChild("spriteLoader").asLoader;
        spriteLoader2.data = 0;

        spriteLoader2.url = "";

        EquipPos pos = dataManager.equipPoses[index];
        InventoryItem item = dataManager.inventoryInfos.Find(x => x.id == pos.id);
        if (item != null)
        {
            //button.GetChild("numText").text = item.slotNum.ToString();
            //����Ӧ����һ����ʱ���ɵ�װ�������϶�

            spriteLoader2.data = item.icon;
            spriteLoader2.url = "ui://CommonPackage/" + item.icon;
            Debug.Log(spriteLoader2.url);
        }
    }

    void OnClickConfirm()
    {
        Debug.Log("sdfdsf");
        m_root.Dispose();
        //Debug.Log("account:" + m_inputAccount.text + "     pwd:" + m_inputPwd.text);
    }

    void OnAddAtk()
    {
        strengh++;
        atkText.text = strengh + "/100";
    }

    void OnAddHp()
    {
        hp++;
        hpText.text = hp + "/100";
    }

    void OnAddMp()
    {
        mp++;
        mpText.text = mp + "/100";
    }

    void OnAddDef()
    {
        def++;
        defText.text = def + "/100";
    }

    public void showCraft()
    {
        _arrangeWindow.Show();
    }
}